{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true
   },
   "source": "import time\nimport click\nimport pandas as pd\nimport numpy as np\nimport math\nfrom typing import List, Tuple\nfrom src.algorithms.clustering_algorithm.duration_limited_clusterer import batch_duration_limited_cluster, \\\n    duration_limited_cluster\nfrom src.algorithms.clustering_algorithm.size_limited_clusterer import SizeLimitedClusterer\nfrom src.services.direct_router.direct_router_rpc_server import DirectRouterRpcServer\nfrom src.services.multi_stop_router.multi_stop_router_rpc_server import MultiStopRouterRPCServer\nfrom src.utils.utils import random_choice\nfrom src.data.data_loader import load_map_data, get_bbox\nfrom src.algorithms.result_visualizer.result_visualizer import ResultVisualizer\nfrom src.algorithms.data_preprocessor.preprocess_data import preprocess_data",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "6d28343ab240b88d",
   "metadata": {},
   "source": "# orders_excel = 'SD_1296.xlsx'\norders_excel = 'SD_3363.xlsx'\nwarehouse_coords = [(32.8663918101245, -117.21455873652934)]  # SD仓库坐标 - 支持多仓库列表格式\n# orders_excel = 'LA_13112.xlsx'\n# warehouse_coords = [(33.95493075987019, -118.37453638829928)]  # LA_AD仓库坐标 - 支持多仓库列表格式\n# 多仓库示例：\n# warehouse_coords = [(32.8663918101245, -117.21455873652934), (33.95493075987019, -118.37453638829928)]  # SD + LA仓库\nper_delivery_duration = 5 * 60  # 默认值设置为 300 秒 (5分钟)\nwork_duration = 10 * 60 * 60  # 默认值设置为 36000 秒 (10小时)",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "8831ed6358f12ddd",
   "metadata": {},
   "source": [
    "# 读取订单数据\n",
    "click.echo(f\"正在读取订单数据...\")\n",
    "df = pd.read_excel(orders_excel, dtype={'business_code': str, 'latitude': float, 'longitude': float})\n",
    "# 提取经纬度坐标\n",
    "points = df[['latitude', 'longitude']].values\n",
    "click.echo(f\"共读取 {len(points)} 个运单。\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "f86c2b36f7c17e73",
   "metadata": {},
   "source": "# 根据仓库坐标获取边界框\nbbox = get_bbox(warehouse_coords, points)\n# 读取地图数据（假设此函数使用仓库坐标）\nclick.echo(f\"加载地图数据...\")\nload_map_data(bbox)",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "7b6ebfb440a53679",
   "metadata": {},
   "source": [
    "# 启动DirectRouterRpcServer\n",
    "direct_router_rpc_server = DirectRouterRpcServer()\n",
    "direct_router_rpc_server.start_server(bbox=bbox)\n",
    "\n",
    "# 启动MultiStopRouterRPCServer\n",
    "multi_stop_router_rpc_server = MultiStopRouterRPCServer()\n",
    "multi_stop_router_rpc_server.start_server(bbox=bbox)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "c880f6cff5cdb877",
   "metadata": {},
   "source": "# 预处理数据\nclick.echo(\"正在预处理数据...\")\npreprocess_data(points, 100)",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "6538bbed5d016272",
   "metadata": {},
   "source": "result_labels = duration_limited_cluster(points, warehouse_coords, per_delivery_duration, work_duration)",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "ce466e27",
   "metadata": {},
   "source": "result_visualizer = ResultVisualizer(points, result_labels, warehouse_coords, per_delivery_duration)\nresult_visualizer.draw_map()",
   "outputs": []
  },
  {
   "cell_type": "code",
   "id": "28058f90",
   "metadata": {},
   "source": [
    "# result_visualizer.export(df)"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "9fb4007b",
   "metadata": {},
   "source": [
    "df2 = result_visualizer.statistical()"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "fed0f047",
   "metadata": {},
   "source": [
    "# 柱状图展示df2的total_working_time字段分布情况\n",
    "df2['total_working_time'].plot(kind='bar', title='Total Working Time Distribution')"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "16924d87",
   "metadata": {},
   "source": [
    "df2['total_working_time'].describe()"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "2b94c388",
   "metadata": {},
   "source": [
    "direct_router_rpc_server.stop_server()\n",
    "multi_stop_router_rpc_server.stop_server()\n",
    "click.echo(\"所有操作完成。\")"
   ],
   "outputs": [],
   "execution_count": null
  },
  {
   "cell_type": "code",
   "id": "8bca1bee",
   "metadata": {},
   "source": [],
   "outputs": [],
   "execution_count": null
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "distribution-planning-fYNSEMLE-py3.11",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}